<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Master extends MX_Controller {
    
  function __construct() {  
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('asset/mmaster');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->load->library('manimage');
    $this->load->library('upload');
    $this->menu = $this->cek();
  }
  public function add_new($id = 0, $pesan = "hal"){
      //
 // echo $tes = $_FILES['image_1']['name']; exit;
    
      
       $config['upload_path'] = './files/asset/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '1000000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->upload->initialize($config);
           $this->upload->do_upload("image_1");
           $this->upload->do_upload("image_2");
           $this->upload->do_upload("image_3");
           
          $pathimage   = "./files/asset/".$_FILES['image_1']['name'];
          $pathimag_b   = "./files/asset/size/".$_FILES['image_1']['name'];
          
           $pathimage2   = "./files/asset/".$_FILES['image_2']['name'];
           $pathimag2_b   = "./files/asset/size/".$_FILES['image_2']['name'];
           
            $pathimage3   = "./files/asset/".$_FILES['image_3']['name'];
            $pathimag3_b   = "./files/asset/size/".$_FILES['image_3']['name'];
            if($_FILES['image_1']['name']){
                $this->manimage->load($pathimage);
                $this->manimage->resizeToWidth(500);
                $this->manimage->resizeToHeight(600);
                $this->manimage->resize(500,600);
                $this->manimage->save($pathimag_b);
            }
            if($_FILES['image_2']['name']){
                $this->manimage->load($pathimage2);
                $this->manimage->resizeToWidth(500);
                $this->manimage->resizeToHeight(600);
                $this->manimage->resize(500,600);
                $this->manimage->save($pathimag2_b);
            }
             if($_FILES['image_3']['name']){
                $this->manimage->load($pathimage3);
                $this->manimage->resizeToWidth(500);
                $this->manimage->resizeToHeight(600);
                $this->manimage->resize(500,600);
                $this->manimage->save($pathimag3_b);
            }
            
      //   echo   $this->upload->display_errors(); exit;

    $this->template->title('Sistem', "Add Edit");
    $kategori = $this->global_models->get_dropdown("m_asset_kategori", "id_asset_kategori", "name", TRUE);
    $brand = $this->global_models->get_dropdown("m_asset_brand", "id_asset_brand", "name", TRUE);
    if($id > 0){
      $data_detail = $this->global_models->get("m_asset", array("id_asset" => $id));

    }
    else{
      $data_detail[0]->id_asset = 0;
      $data_detail[0]->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('master/add-new-master', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Master Asset',
              'detail'  => $data_detail,
            'id_asset_kategori'  => $kategori,
            'id_asset_brand'  => $brand,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
         
            ));
      $this->template
        ->set_layout('default')
        ->build('master/add-new-master');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
          
          if($_FILES['image_1']['name']){
              $fileimage = $_FILES['image_1']['name'] ;
            }else{
                $fileimage = $this->input->post("load_image_1", TRUE) ;
            }
            if($_FILES['image_2']['name']){
              $fileimage2 = $_FILES['image_2']['name'] ;
            }else{
                $fileimage2 = $this->input->post("load_image_2", TRUE) ;
            }
             if($_FILES['image_3']['name']){
              $fileimage3 = $_FILES['image_3']['name'] ;
            }else{
                $fileimage3 = $this->input->post("load_image_3", TRUE) ;
            }
            
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
             "id_asset_kategori"                  =>  $this->input->post("id_asset_kategori", TRUE),
              "id_asset_brand"                  =>  $this->input->post("id_asset_brand", TRUE),
              "kode"                  =>  $this->input->post("kode", TRUE),
            "panjang"                  =>  $this->input->post("panjang", TRUE),
              "lebar"                  =>  $this->input->post("lebar", TRUE),
            "tinggi"                  =>  $this->input->post("tinggi", TRUE),
              "lebar"                  =>  $this->input->post("lebar", TRUE),
            "massa"                  =>  $this->input->post("massa", TRUE),
              "note"                  =>  $this->input->post("note", TRUE),
              "update_by_users"       =>  $this->session->userdata('id'),
            "image_1"       =>  $fileimage,
            "image_2"       =>  $fileimage2,
            "image_3"       =>  $fileimage3,
          );
        $hasil_in = $this->global_models->update("m_asset", array("id_asset" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("asset/master/index/list/sukses");
        }
        else
          redirect ("asset/master/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "image_1"                   => $_FILES['image_1']['name'],
              "image_2"                   => $_FILES['image_2']['name'],
              "image_3"                   => $_FILES['image_3']['name'],
              "kode"                  =>  $this->input->post("kode", TRUE),
              "id_asset_kategori"                  =>  $this->input->post("id_asset_kategori", TRUE),
              "id_asset_brand"                  =>  $this->input->post("id_asset_brand", TRUE),
            "panjang"                  =>  $this->input->post("panjang", TRUE),
              "lebar"                  =>  $this->input->post("lebar", TRUE),
            "tinggi"                  =>  $this->input->post("tinggi", TRUE),
              "lebar"                  =>  $this->input->post("lebar", TRUE),
            "massa"                  =>  $this->input->post("massa", TRUE),
              "note"                  =>  $this->input->post("note", TRUE),
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
          );
       
          if($this->global_models->insert("m_asset", $kirim))
            redirect ("asset/master/index/list/sukses");
          else{
            redirect ("asset/master/index/list/filed");
          }
        }
        else{
          redirect ("asset/master/index/list/filed");
        }
      }
    }
  }
  
  /**
   * ini delete
   * @param int $id id yang didelete
   * @return true redirect
   * @
   */
	public function delete($id){
    if($this->global_models->delete("m_asset", array("id_asset" => $id)))
      redirect("asset/master/index/list/sukses");
    else
      redirect("asset/master/index/list/filed");
  }
	public function export_xls(){
    $this->mmaster->export_xls("data-asset");
  }
public function index($action = "list", $pesan = "hal", $hal = 0){
    $list = $this->mmaster->get("id_asset","m_asset");
    
    $this->template->title('Sistem', "Master Asset");
     
//    
    $this->template->build('master/main', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'data'        => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table' => "Menu Master Asset Table",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('master/main');
	} 
        
    public function kategori($action = "list", $pesan = "hal", $hal = 0){
    $list = $this->mmaster->get("id_asset_kategori","m_asset_kategori");
    $this->template->title('Sistem', "Master Asset Kategori");
    
    $this->template->build('master/asset-kategori', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'data'        => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table' => "Menu Master Asset Kategori Table",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('master/asset-kategori');
	}
     public function add_kategori($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Add Edit");
    if($id > 0){
      $data_detail = $this->global_models->get("m_asset_kategori", array("id_asset_kategori" => $id));

    }
    else{
      $data_detail[0]->id_asset_kategori = 0;
      $data_detail[0]->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('master/add-asset-kategori', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Master Asset Kategori',
              'detail'  => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
         
            ));
      $this->template
        ->set_layout('default')
        ->build('master/add-asset-kategori');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "update_by_users"       =>  $this->session->userdata('id')
          );
        $hasil_in = $this->global_models->update("m_asset_kategori", array("id_asset_kategori" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("asset/master/kategori/index/list/sukses");
        }
        else
          redirect ("asset/master/kategori/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE), 
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
          );
          if($this->global_models->insert("m_asset_kategori", $kirim))
            redirect ("asset/master/kategori/index/list/sukses");
          else{
            redirect ("asset/master/kategori/index/list/filed");
          }
        }
        else{
          redirect ("asset/master/kategori/index/list/filed");
        }
      }
    }
  }
  	public function delete_kategori($id){
    if($this->global_models->delete("m_asset_kategori", array("id_asset_kategori" => $id)))
      redirect("asset/master/kategori/index/list/sukses");
    else
      redirect("asset/master/kategori/index/list/filed");
  }
  
  public function brand($action = "list", $pesan = "hal", $hal = 0){
    $list = $this->mmaster->get("id_asset_brand","m_asset_brand");
    $this->template->title('Sistem', "Master Asset Brand");
    
    $this->template->build('master/asset-brand', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'data'        => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table' => "Menu Master Asset Brand Table",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('master/asset-brand');
	}
        
        public function add_brand($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Add Edit");
    if($id > 0){
      $data_detail = $this->global_models->get("m_asset_brand", array("id_asset_brand" => $id));

    }
    else{
      $data_detail[0]->id_asset_brand = 0;
      $data_detail[0]->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('master/add-asset-brand', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Master Asset Brand',
              'detail'  => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
         
            ));
      $this->template
        ->set_layout('default')
        ->build('master/add-asset-brand');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "update_by_users"       =>  $this->session->userdata('id')
          );
        $hasil_in = $this->global_models->update("m_asset_brand", array("id_asset_brand" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("asset/master/brand/index/list/sukses");
        }
        else
          redirect ("asset/master/brand/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE), 
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
          );
          if($this->global_models->insert("m_asset_brand", $kirim))
            redirect ("asset/master/brand/index/list/sukses");
          else{
            redirect ("asset/master/brand/index/list/filed");
          }
        }
        else{
          redirect ("asset/master/brand/index/list/filed");
        }
      }
    }
  }
  
  public function delete_brand($id){
    if($this->global_models->delete("m_asset_brand", array("id_asset_brand" => $id)))
      redirect("asset/master/brand/index/list/sukses");
    else
      redirect("asset/master/brand/index/list/filed");
  }

  public function gudang($action = "list", $pesan = "hal", $hal = 0){
      
    $list = $this->mmaster->get("id_asset_gudang","m_asset_gudang");
    $this->template->title('Sistem', "Master Gudang");
    
    $this->template->build('master/gudang', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'data'        => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table' => "Menu Master Gudang Table",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('master/gudang');
	}
        
         public function add_gudang($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Add Edit");
    if($id > 0){
      $data_detail = $this->global_models->get("m_asset_gudang", array("id_asset_gudang" => $id));

    }
    else{
      $data_detail[0]->id_asset_gudang = 0;
      $data_detail[0]->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('master/add-gudang', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Master Gudang',
              'detail'  => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
         
            ));
      $this->template
        ->set_layout('default')
        ->build('master/add-gudang');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
            "address"                  =>  $this->input->post("address", TRUE),
              "update_by_users"       =>  $this->session->userdata('id')
          );
        $hasil_in = $this->global_models->update("m_asset_gudang", array("id_asset_gudang" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("asset/master/gudang/index/list/sukses");
        }
        else
          redirect ("asset/master/gudang/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "address"                  =>  $this->input->post("address", TRUE),
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
          );
          if($this->global_models->insert("m_asset_gudang", $kirim))
            redirect ("asset/master/gudang/index/list/sukses");
          else{
            redirect ("asset/master/gudang/index/list/filed");
          }
        }
        else{
          redirect ("asset/master/gudang/index/list/filed");
        }
      }
    }
  }
  
    public function delete_gudang($id){
    if($this->global_models->delete("m_asset_gudang", array("id_asset_gudang" => $id)))
      redirect("asset/master/gudang/index");
    else
      redirect("asset/master/gudang/index/");
  }
  
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */